<?php

namespace App\Library\Logic;

use App\Library\Requests\Handbook\HandbookAddRequest;
use App\Library\Requests\Handbook\HandbookPageRequest;
use App\Models\ShopHandbookModel;

class ShopHandbookLogic extends ShopHandbookModel
{
    /**
     * paging
     * @param HandbookPageRequest $request
     * @return array
     */
    public function paging(HandbookPageRequest $request): array
    {
        return paginate(helpBuilder($this->newQuery()->orderByDesc('hb_id'), $request)->paginate($request->limit));
    }

    /**
     * saveCom
     * @param HandbookAddRequest $request
     * @return array
     */
    public function saveCom(HandbookAddRequest $request): array
    {
        $array = handleRequest($request);
        foreach ($array as $k => $v) {
            if ($k !== 'fileIds') {
                $array['hb' . ucfirst($k)] = $v;
                unset($array[$k]);
            }
        }
        $array['updatedAt'] = time();
        return $array;
    }
}
